package com.mm.service.impl;

import com.mm.bo.CategoryInsertBo;
import com.mm.bo.CategorySearchBo;
import com.mm.bo.CategoryUpdateBo;
import com.mm.mapper.CategoryInfoMapper;
import com.mm.mapper.LogsInfoMapper;
import com.mm.org.n3r.idworker.Sid;
import com.mm.pojo.AdministratorInfo;
import com.mm.pojo.CategoryInfo;
import com.mm.pojo.LogsInfo;
import com.mm.redis.RedisService;
import com.mm.rediskey.AdminKey;
import com.mm.service.CategoryInfoService;
import com.mm.util.PageUtil;
import com.mm.vo.CategoryInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName CategoryInfoServiceImpl
 * @Description TODO
 * @Author 任昊
 * @Date 2020/12/7 10:14
 */
@Service
public class CategoryInfoServiceImpl implements CategoryInfoService {

    @Resource
    private CategoryInfoMapper categoryInfoMapper;

    @Resource
    private Sid sid;

    @Resource
    private RedisService redisService;

    @Resource
    private LogsInfoMapper logsInfoMapper;

    /**
     * 新增物资种类
     *
     * @param categoryInsertBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertCategoryInfo(CategoryInsertBo categoryInsertBo) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, categoryInsertBo.getToken(), AdministratorInfo.class);

        CategoryInfo categoryInfo = new CategoryInfo();
        BeanUtils.copyProperties(categoryInsertBo, categoryInfo);
        long currentTimeMillis = System.currentTimeMillis();
        categoryInfo.setCateId(sid.nextShort());
        categoryInfo.setCateCreateTime(currentTimeMillis);
        categoryInfo.setCateUpdateTime(currentTimeMillis);
        int row = categoryInfoMapper.insertSelective(categoryInfo);

        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "新增了物资种类：" + categoryInsertBo.getCateName();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, currentTimeMillis, currentTimeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return row;
    }

    /**
     * 修改物资种类
     *
     * @param categoryUpdateBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer UpdateCategoryInfo(CategoryUpdateBo categoryUpdateBo) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, categoryUpdateBo.getToken(), AdministratorInfo.class);

        CategoryInfo categoryInfo = new CategoryInfo();
        BeanUtils.copyProperties(categoryUpdateBo, categoryInfo);
        Example example = new Example(CategoryInfo.class);
        example.createCriteria().andEqualTo("cateId", categoryUpdateBo.getCateId());
        int row = categoryInfoMapper.updateByExampleSelective(categoryInfo, example);

        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "修改了物资种类：" + categoryUpdateBo.getCateId();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return row;
    }

    /**
     * 删除物资种类
     *
     * @param cateId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteCategoryInfo(String cateId, String token) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, token, AdministratorInfo.class);

        int rowCate = categoryInfoMapper.deleteByPrimaryKey(cateId);
        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "删除了物资种类：" + cateId;
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return rowCate;
    }

    /**
     * 查询物资种类
     *
     * @param categorySearchBo
     * @return
     */
    @Override
    public PageUtil<CategoryInfoVo> selectCategoryInfo(CategorySearchBo categorySearchBo) {
        Integer count = categoryInfoMapper.findCategoryCount(categorySearchBo);
        List<CategoryInfoVo> categoryList = categoryInfoMapper.findCategoryList(categorySearchBo);
        PageUtil<CategoryInfoVo> pageUtil = new PageUtil<>(count, count, categorySearchBo.getPageIndex(), categorySearchBo.getPageSize(), categoryList);
        return pageUtil;
    }
}
